﻿using DocumentFormat.OpenXml.Drawing.Spreadsheet;
using DocumentFormat.OpenXml.ExtendedProperties;
using DocumentFormat.OpenXml.Spreadsheet;
using LView.CMS.Core.Extensions;
using LView.CMS.Core.Helper;
using LView.CMS.Core.Models;
using LView.CMS.IServices;
using LView.CMS.Site.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Claims;

namespace LView.CMS.Site.Controllers
{
    public class HomeController : BaseController
    {
        private readonly IManagerRoleService _ManagerRoleService;

        public HomeController(IManagerRoleService ManagerRoleService, IHttpContextAccessor httpContextAccessor)
        {
            _ManagerRoleService = ManagerRoleService;
        }

        public IActionResult Index()
        {
            if (BaseController.CurrentUser == null)
                BaseController.CurrentUser = new Core.Models.Operator
                {
                    UserId = User.Claims.FirstOrDefault(x => x.Type == "ID")?.Value,
                    NickName = User.Claims.FirstOrDefault(x => x.Type == "NickName")?.Value,
                    UserName = User.Claims.FirstOrDefault(x => x.Type == "UserName")?.Value,
                    RoleId = User.Claims.FirstOrDefault(x => x.Type == "RoleID")?.Value,
                    Avatar = User.Claims.FirstOrDefault(x => x.Type == "Avatar")?.Value
                };
            ViewData["NickName"] = CurrentUser.NickName;
            ViewData["Avatar"] = CurrentUser.Avatar;
            return View();
        }

        public IActionResult Main()
        {
            ViewData["LoginCount"] = User.Claims.FirstOrDefault(x => x.Type == "LoginCount")?.Value;
            ViewData["LoginLastIp"] = User.Claims.FirstOrDefault(x => x.Type == "LoginLastIp")?.Value;
            ViewData["LoginLastTime"] = User.Claims.FirstOrDefault(x => x.Type == "LoginLastTime")?.Value;
            return View();
        }

        public string getmenu()
        {
            var list = _ManagerRoleService.GetMenusByRoleId();
            list.Where(x => x.ParentId == "0").ToList().ForEach(x => { x.ParentId = null; });
            var navviewtree = list.GenerateTree(x => x.Id, x => x.ParentId);
            return JsonHelper.ObjectToJson(navviewtree);
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
